using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using TXLooker.WorkItemExtention.Core;

namespace TXLooker.WorkItemExtention.UI
{
    internal partial class WorkItemTypeSelectionForm : Form
    {
        public WIProjectProvider ProjectProvider
        {
            get { return this.m_projectProvider; }
            internal set
            {
                this.m_projectProvider = value;
            }
        }
        private WIProjectProvider m_projectProvider = null;

        public WorkItemType WorkItemType
        {
            get { return this.m_cbWorkItemTypes.SelectedItem as WorkItemType; }
        }

        private List<WIServerProvider> Servers
        {
            get { return this.m_servers; }
        }
        private List<WIServerProvider> m_servers = new List<WIServerProvider>();

        #region Constructors

        private WorkItemTypeSelectionForm()
        {
            InitializeComponent();

            this.m_tvProjects.AfterSelect += new TreeViewEventHandler( OnAfterSelectNode );
        }
        public WorkItemTypeSelectionForm( IEnumerable<WIServerProvider> servers, WIProjectProvider current ) : this()
        {
            this.Servers.AddRange( servers );
            this.ProjectProvider = current;
        }

        #endregion Constructors

        protected override void OnLoad( EventArgs e )
        {
            base.OnLoad(e);
            LoadTree( this.m_tvProjects, this.Servers, this.ProjectProvider );
        }

        private void LoadTree( TreeView treeView, IEnumerable<WIServerProvider> servers, WIProjectProvider currentProject )
        {
            if( object.ReferenceEquals(treeView, null) || object.ReferenceEquals(servers, null) )
                throw new ArgumentException();

            TreeNode selectedNode = null;

            foreach( WIServerProvider server in servers )
            {
                TreeNode srvNode = new TreeNode( string.Format("Server: {0}",server.ServerName) );
                srvNode.Tag = server;

                foreach( WIProjectProvider project in server.Projects )
                {
                    Project tfProject = null;

                    try   { tfProject = project.GetProject(); }
                    catch { }

                    if( tfProject == null )
                        continue;

                    TreeNode projectNode = new TreeNode( string.Format("Project: {0}",tfProject.Name) );
                    projectNode.Tag = project;
                    srvNode.Nodes.Add( projectNode );

                    if( !object.ReferenceEquals(currentProject, null) )
                    {
                        if( object.ReferenceEquals( project, currentProject ) )
                            selectedNode = projectNode;
                    }
                }

                treeView.Nodes.Add( srvNode );

                if( selectedNode != null )
                    treeView.SelectedNode = selectedNode;
            }
        }

        private void OnAfterSelectNode( object sender, TreeViewEventArgs e )
        {
            this.ProjectProvider = e.Node.Tag as WIProjectProvider;
            this.m_btnAdd.Enabled = (ProjectProvider != null);
            this.m_cbWorkItemTypes.Enabled = this.m_btnAdd.Enabled;

            this.m_cbWorkItemTypes.DataSource = null;

            if( this.ProjectProvider == null )
                return;

            Project project = null;

            try   { project = this.ProjectProvider.GetProject(); }
            catch { }

            if( project == null )
                return;

            try
            {
                this.m_cbWorkItemTypes.DataSource = project.WorkItemTypes;
                this.m_cbWorkItemTypes.DisplayMember = "Name";
            }
            catch
            { }
        }

    }
}